package com.smartum.mpr.client.presenter;

import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartum.mpr.client.event.LanguageChangeHandler;
import com.smartum.mpr.client.event.LoginButtonHandler;

/**
 * Presenter class for login UI.
 *
 * @see LanguageChangeHandler
 */
public class LoginPresenter implements Presenter {
    public interface Display {
        Widget getLoginWidget();

        SelectItem getLanguageItem();

        TextItem getUsernameItem();

        PasswordItem getPasswordItem();

        IButton getLoginButton();

        DynamicForm getForm();
    }

    private final Display display;
    private HandlerManager eventBus;

    public LoginPresenter(Display display, HandlerManager eventBus) {
        this.display = display;
        this.eventBus = eventBus;
    }

    public void go(HasWidgets container) {
        container.clear();
        container.add(this.display.getLoginWidget());
    }

    public void bind() {
        this.display.getLanguageItem().addChangedHandler(new LanguageChangeHandler());
        this.display.getLoginButton().addClickHandler(new LoginButtonHandler(this));
    }

    public HandlerManager eventBus() {
        return this.eventBus;
    }

    public Display getDisplay() {
        return this.display;
    }
}
